package com.escom.service;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.escom.dao.PacienteDAO;
import com.escom.entidad.PacienteVO;
import com.escom.utils.Constantes;

import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Carlos on 7/04/14.
 */
public class PacienteWebService implements Runnable {

    public static final int CREAR_PACIENTE = 0;
    public static final int CONSULTAR_PACIENTE = 1;

    private Handler handler;
    private Context context;
    private String idMedico;

    private int opc;

    public PacienteWebService(int opc) {
        this.opc = opc;
    }

    @Override
    public void run() {
        switch (opc) {
            case CREAR_PACIENTE:
                break;
            case CONSULTAR_PACIENTE:
                syncPacientes();
                break;
        }
    }

    private void syncPacientes() {
        Message msgObj = handler.obtainMessage();
        Bundle bundleMessage = new Bundle();
        int messageResponse = -1;
        String url = "http://" + Constantes.IP_PC + ":8080/TrabajoTerminal-web/rest/pacienteWebService?" +
                "idMedico={query}";
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

        ClientHttpRequestFactory clientHttpRequestFactory = restTemplate.getRequestFactory();
        int timeout = 10000;
        if (clientHttpRequestFactory instanceof SimpleClientHttpRequestFactory) {
            ((SimpleClientHttpRequestFactory) clientHttpRequestFactory).setConnectTimeout(timeout);
            ((SimpleClientHttpRequestFactory) clientHttpRequestFactory).setReadTimeout(timeout);
        } else if (clientHttpRequestFactory instanceof HttpComponentsClientHttpRequestFactory) {
            ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory).setReadTimeout(timeout);
            ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory).setConnectTimeout(timeout);
        }
        try{
            PacienteVO[] resultPaciente = restTemplate.getForObject(url, PacienteVO[].class, idMedico);
            PacienteDAO pacienteDAO = new PacienteDAO(context);
            pacienteDAO.setIdMedico(idMedico);
            pacienteDAO.createPacientes(resultPaciente);
        }catch(ResourceAccessException exResource){
            Log.e("Logger Error: {}", exResource.fillInStackTrace().toString());
            messageResponse = Constantes.ERROR_COMUNICACION;
        }
        if (messageResponse == -1){
            messageResponse = Constantes.EXITO;
        }
        bundleMessage.putInt("message", messageResponse);
        msgObj.setData(bundleMessage);
        handler.sendMessage(msgObj);
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setIdMedico(String idMedico) {
        this.idMedico = idMedico;
    }
}
